$(function(){
	
	var data = $("#tabContent > .tab-pane.active").data();
	var depGroupCode = data.depGroupCode;
	var dateFrom = data.dateFrom;
	var dateTo = data.dateTo;
	var param = data.param;
	var AvgDaysL=0;
	var AvgDaysH=0;
	var AvgFeeL=0;
	var AvgFeeH=0;
	var HCAvgFeeL=0;
	var HCAvgFeeH=0;
	var DrugFeeL=0;
	var DrugFeeH=0;
	var HCDrugFeeL=0;
	var HCDrugFeeH=0;
	var DrugMateFeeL=0;
	var DrugMateFeeH=0;
	var HCDrugMateFeeL=0;
	var HCDrugMateFeeH=0;
	var array = param.split("-");
	for(var i=0; i<array.length; i++){
		var as = array[i].split(":");
		if(as[0]=="AvgDays"){
			AvgDaysL=as[1];
			AvgDaysH=as[2];
		}
		if(as[0]=="AvgFee"){
			AvgFeeL=as[1];
			AvgFeeH=as[2];
		}
		if(as[0]=="HCAvgFee"){
			HCAvgFeeL=as[1];
			HCAvgFeeH=as[2];
		}
		if(as[0]=="DrugFee"){
			DrugFeeL=as[1];
			DrugFeeH=as[2];
		}
		if(as[0]=="HCDrugFee"){
			HCDrugFeeL=as[1];
			HCDrugFeeH=as[2];
		}
		if(as[0]=="DrugMateFee"){
			DrugMateFeeL=as[1];
			DrugMateFeeH=as[2];
		}
		if(as[0]=="HCDrugMateFee"){
			HCDrugMateFeeL=as[1];
			HCDrugMateFeeH=as[2];
		}
	}
	
	$.post(contextPath+"/ForeCast/loadDepParam.dhc", {
		pardepgroupCode:depGroupCode,
		type2:planYear,
		roleName:qouta_roles,
		dateFrom:dateFrom,
		dateTo:dateTo
		})
	.done(function(result){
		var columns = [
				{
					"data" : 'DepDesc1',
					"render": function(obj, type, full) {
						return "<input type='checkbox' />  "
								+ full.depDesc;
					}
				},
				{
					"data" : 'totalCaseCnt'
				},
				{
					"data" : "AvgDaysL",
					"render": function(obj, type, full) {
						var avgDaysL = full.AvgDaysL ? full.AvgDaysL
								: AvgDaysL;
						return "<input id='"
								+ full.depCode
								+ "_AvgDaysL' type='text' style='width:50px' value='"
								+ avgDaysL + "'></input>";
					}
				},
				{
					"data" : "AvgDaysH",
					"render": function(obj, type, full) {
						var avgDaysH = full.AvgDaysH ? full.AvgDaysH
								: AvgDaysH;
						return "<input id='"
								+ full.depCode
								+ "_AvgDaysH' type='text' style='width:50px' value='"
								+ avgDaysH + "'></input>";
					}
				},
				{
					"data" : "AvgFeeL",
					"render": function(obj, type, full) {
						var avgFeeL = full.AvgFeeL ? full.AvgFeeL
								: AvgFeeL;
						return "<input id='"
								+ full.depCode
								+ "_AvgFeeL' type='text' style='width:50px' value='"
								+ avgFeeL + "'></input>";
					}
				},
				{
					"data" : "AvgFeeH",
					"render": function(obj, type, full) {
						var avgFeeH = full.AvgFeeH ? full.AvgFeeH
								: AvgFeeH;
						return "<input id='"
								+ full.depCode
								+ "_AvgFeeH' type='text' style='width:50px' value='"
								+ avgFeeH + "'></input>";
					}
				},
				{
					"data" : "HCAvgFeeL",
					"render": function(obj, type, full) {
						var hcAavgFeeL = full.HCAvgFeeL ? full.HCAvgFeeL
								: HCAvgFeeL;
						return "<input id='"
								+ full.depCode
								+ "_HCAvgFeeL' type='text' style='width:50px' value='"
								+ hcAavgFeeL + "'></input>";
					}
				},
				{
					"data" : "HCAvgFeeH",
					"render": function(obj, type, full) {
						var hcAavgFeeH = full.HCAvgFeeH ? full.HCAvgFeeH
								: HCAvgFeeH;
						return "<input id='"
								+ full.depCode
								+ "_HCAvgFeeH' type='text' style='width:50px' value='"
								+ hcAavgFeeH + "'></input>";
					}
				},
				{
					"data" : "DrugFeeL",
					"render": function(obj, type, full) {
						var drugFeeL = full.DrugFeeL ? full.DrugFeeL
								: DrugFeeL;
						return "<input id='"
								+ full.depCode
								+ "_DrugFeeL' type='text' style='width:50px' value='"
								+ drugFeeL + "'></input>";
					}
				},
				{
					"data" : "DrugFeeH",
					"render": function(obj, type, full) {
						var drugFeeH = full.DrugFeeH ? full.DrugFeeH
								: DrugFeeH;
						return "<input id='"
								+ full.depCode
								+ "_DrugFeeH' type='text' style='width:50px' value='"
								+ drugFeeH + "'></input>";
					}
				},
				{
					"data" : "HCDrugFeeL",
					"render": function(obj, type, full) {
						var hcDrugFeeL = full.HCDrugFeeL ? full.HCDrugFeeL
								: HCDrugFeeL;
						return "<input id='"
								+ full.depCode
								+ "_HCDrugFeeL' type='text' style='width:50px' value='"
								+ hcDrugFeeL + "'></input>";
					}
				},
				{
					"data" : "HCDrugFeeH",
					"render": function(obj, type, full) {
						var hcDrugFeeH = full.HCDrugFeeH ? full.HCDrugFeeH
								: HCDrugFeeH;
						return "<input id='"
								+ full.depCode
								+ "_HCDrugFeeH' type='text' style='width:50px' value='"
								+ hcDrugFeeH + "'></input>";
					}
				},
				{
					"data" : "DrugMateFeeL",
					"render": function(obj, type, full) {
						var drugMateFeeL = full.DrugMateFeeL ? full.DrugMateFeeL
								: DrugMateFeeL;
						return "<input id='"
								+ full.depCode
								+ "_DrugMateFeeL' type='text' style='width:50px' value='"
								+ drugMateFeeL + "'></input>";
					}
				},
				{
					"data" : "DrugMateFeeH",
					"render": function(obj, type, full) {
						var drugMateFeeH = full.DrugMateFeeH ? full.DrugMateFeeH
								: DrugMateFeeH;
						return "<input id='"
								+ full.depCode
								+ "_DrugMateFeeH' type='text' style='width:50px' value='"
								+ drugMateFeeH + "'></input>";
					}
				},
				{
					"data" : "HCDrugMateFeeL",
					"render": function(obj, type, full) {
						var hcDrugMateFeeL = full.HCDrugMateFeeL ? full.HCDrugMateFeeL
								: HCDrugMateFeeL;
						return "<input id='"
								+ full.depCode
								+ "_HCDrugMateFeeL' type='text' style='width:50px' value='"
								+ hcDrugMateFeeL + "'></input>";
					}
				},
				{
					"data" : "HCDrugMateFeeH",
					"render": function(obj, type, full) {
						var hcDrugMateFeeH = full.HCDrugMateFeeH ? full.HCDrugMateFeeH
								: HCDrugMateFeeH;
						return "<input id='"
								+ full.depCode
								+ "_HCDrugMateFeeH' type='text' style='width:50px' value='"
								+ hcDrugMateFeeH + "'></input>";
					}
				}
				];
		

		var paramDatatable = {
		lengthChange : false,
		info : false,
		searching : false,
		order : [ [ 1, "desc" ] ],
		paging : false,
		scrollX:false,
		scrollY:400	
		};
		if(qouta_roles!="医保"){
			paramDatatable.columnDefs=[{
				 "visible": false,
				  "targets": [6,7,10,11,14,15]
	         }]
		}
		
		paramTable = $("#"+tabId+" #paramDep-table").dataTableBasic(columns, result.data.data, paramDatatable);
			
		$("#"+tabId+" #mergeParamOk").click(function(){
			$("#"+tabId+" #paramDep-table tbody").find("tr").each(function(i, n){
				if(qouta_roles=="医政"){
					if($("#"+tabId+" #paramQuotaCheck").val()=="AvgDays"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(2).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(3).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="AvgFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(4).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(5).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="DrugFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(6).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(7).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}

					if($("#"+tabId+" #paramQuotaCheck").val()=="DrugMateFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(8).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(9).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
				}else{
					if($("#"+tabId+" #paramQuotaCheck").val()=="AvgDays"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(2).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(3).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="AvgFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(4).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(5).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="HCAvgFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(6).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(7).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="DrugFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(8).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(9).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="HCDrugFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(10).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(11).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="DrugMateFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(12).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(13).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="HCDrugMateFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							$(n).find("td").eq(14).find("input").attr("value",$("#"+tabId+" #overManageID").val());
							$(n).find("td").eq(15).find("input").attr("value",$("#"+tabId+" #notOverManageID").val());
						}
					}
				}
			})
			drgUI.success("修改成功") ;
		})
	});
	
	$("#"+tabId+" #depParamSave_btn").click(function(){
		var str = "";
		var rows = paramTable.columns().context[0].aoData;
		$.each(rows, function(i, n){
			n=n.anCells;
			var depGroupCode=$(n[2]).find("input").attr("id")?$(n[2]).find("input").attr("id").split("_")[0]:"";
			str += depGroupCode+"|";
			if(qouta_roles=="医政"){
				var avgDaysL=$(n[2]).find("input").val();
				var avgDaysH=$(n[3]).find("input").val();
				str += "AvgDays:"+avgDaysL+":"+avgDaysH;
				str += "-";
				var avgFeeL=$(n[4]).find("input").val();
				var avgFeeH=$(n[5]).find("input").val();
				str += "AvgFee:"+avgFeeL+":"+avgFeeH;
				str += "-";
				var drugFeeL=$(n[8]).find("input").val();
				var drugFeeH=$(n[9]).find("input").val();
				str += "DrugFee:"+drugFeeL+":"+drugFeeH;
				str += "-";
				var drugMateFeeL=$(n[12]).find("input").val();
				var drugMateFeeH=$(n[13]).find("input").val();
				str += "DrugMateFee:"+drugMateFeeL+":"+drugMateFeeH;
			}else{
				var avgDaysL=$(n[2]).find("input").val();
				var avgDaysH=$(n[3]).find("input").val();
				str += "AvgDays:"+avgDaysL+":"+avgDaysH;
				str += "-";
				var avgFeeL=$(n[4]).find("input").val();
				var avgFeeH=$(n[5]).find("input").val();
				str += "AvgFee:"+avgFeeL+":"+avgFeeH;
				str += "-";
				var hcavgFeeL=$(n[6]).find("input").val();
				var hcavgFeeH=$(n[7]).find("input").val();
				str += "HCAvgFee:"+hcavgFeeL+":"+hcavgFeeH;
				str += "-";
				var drugFeeL=$(n[8]).find("input").val();
				var drugFeeH=$(n[9]).find("input").val();
				str += "DrugFee:"+drugFeeL+":"+drugFeeH;
				str += "-";
				var hcdrugFeeL=$(n[10]).find("input").val();
				var hcdrugFeeH=$(n[11]).find("input").val();
				str += "HCDrugFee:"+hcdrugFeeL+":"+hcdrugFeeH;
				str += "-";
				var drugMateFeeL=$(n[12]).find("input").val();
				var drugMateFeeH=$(n[13]).find("input").val();
				str += "DrugMateFee:"+drugMateFeeL+":"+drugMateFeeH;
				str += "-";
				var hcdrugMateFeeL=$(n[14]).find("input").val();
				var hcdrugMateFeeH=$(n[15]).find("input").val();
				str += "HCDrugMateFee:"+hcdrugMateFeeL+":"+hcdrugMateFeeH;
			}
			str += ",";
		})
		str = str.substr(0, str.length-1)
		$.post(contextPath+"/ForeCast/saveParamDepData.dhc", {"type1": str,type2:planYear,"roleName":qouta_roles})
		.done(function(result){
			if(result.success){
				drgUI.success("保存完毕") ;
			}
		});
	});
	
	$("#"+tabId+" #date-from-1").html(dateFrom);
	$("#"+tabId+" #date-to-1").html(dateTo);
	$.post(contextPath+"/DrgsIndexCal/indexCal.dhc", 
	{
		'pardepgroupCode': depGroupCode,
		'dateFrom':dateFrom,
		'dateTo':dateTo,
		'quotaCode':"ForeCast",
		'monitorType':"depGroup",
		'msCode':"std_inner",
		'compareType':"std",
		'gType': "1"
	}).done(function(result){
		$.post(contextPath+"/DrgsIndexCal/indexCal.dhc", 
		{
			'pardepgroupCode': depGroupCode,
			'dateFrom':dateFrom,
			'dateTo':dateTo,
			'quotaCode':"ForeCast",
			'monitorType':"dep",
			'msCode':"std_inner",
			'compareType':"std",
			'gType': "1"
		}).done(function(result_1){
			var data = [];
			var hospData={};
			if(result.data && result.data.length>0){
				hospData = result.data[0];
				data.push(hospData);
			}
			if(result_1.data && result_1.data.length>0){
				data = data.concat(result_1.data);
			}
			var columns = [ 
		                     { "data": 'ddesc'},
		                     { "data": 'TotalCaseCnt'},
		                     { "data": 'HCTotalCaseCnt'},
		                     { "data": 'HCTotalFund', 
			                       "render": function(obj, type, full) {
			                    	   return formatNumber(full.HCTotalFund/10000,3) ;
			                       }
		                     },
		                     { "data": 'AvgDays'},
		                     { "data": 'AvgDayscc'},
		                     { "data": 'AvgFee'},
		                     { "data": 'AvgFeecc'},
		                     { "data": 'HCAvgFee'},
		                     { "data": 'HCAvgFeecc'},
		                     { "data": 'AvgDrugFee'},
		                     { "data": 'AvgDrugFeecc'},
		                     { "data": 'HCAvgDrugFee'},
		                     { "data": 'HCAvgDrugFeecc'},
		                     { "data": 'AvgDrugMateFee'},
		                     { "data": 'AvgDrugMateFeecc'},
		                     { "data": 'HCAvgDrugMateFee'},
		                     { "data": 'HCAvgDrugMateFeecc'}
		                 ];
			var paramDatatable = {lengthChange:false, info:false, searching:false, order:[[ 1, "desc" ]]};
			if(qouta_roles!="医保"){
				paramDatatable.columnDefs=[{
					 "visible": false,
					  "targets": [3,5,6,7,9,13]
			    }]
			}
			$("#"+tabId+" #foredatadep-table").dataTableBasic(columns, data, paramDatatable);
		});
	});
})

function goToDisParam(depCode,depDesc){
	var str="";
	$("#"+tabId+" #paramDep-table tbody").find("tr").each(function(i, n){
		var depCodes=$(n).find("td").eq(2).find("input").attr("id")?$(n).find("td").eq(2).find("input").attr("id").split("_")[0]:"";
		if(depCodes==depCode){
			var avgDaysL=$(n).find("td").eq(2).find("input").val();
			var avgDaysH=$(n).find("td").eq(3).find("input").val();
			str += "AvgDays:"+avgDaysL+":"+avgDaysH;
			str += "-";
			var avgFeeL=$(n).find("td").eq(4).find("input").val();
			var avgFeeH=$(n).find("td").eq(5).find("input").val();
			str += "AvgFee:"+avgFeeL+":"+avgFeeH;
			str += "-";
			var hcavgFeeL=$(n).find("td").eq(6).find("input").val();
			var hcavgFeeH=$(n).find("td").eq(7).find("input").val();
			str += "HCAvgFee:"+hcavgFeeL+":"+hcavgFeeH;
			str += "-";
			var drugFeeL=$(n).find("td").eq(8).find("input").val();
			var drugFeeH=$(n).find("td").eq(9).find("input").val();
			str += "DrugFee:"+drugFeeL+":"+drugFeeH;
			str += "-";
			var hcdrugFeeL=$(n).find("td").eq(10).find("input").val();
			var hcdrugFeeH=$(n).find("td").eq(11).find("input").val();
			str += "HCDrugFee:"+hcdrugFeeL+":"+hcdrugFeeH;
			str += "-";
			var drugMateFeeL=$(n).find("td").eq(12).find("input").val();
			var drugMateFeeH=$(n).find("td").eq(13).find("input").val();
			str += "DrugMateFee:"+drugMateFeeL+":"+drugMateFeeH;
			str += "-";
			var hcdrugMateFeeL=$(n).find("td").eq(14).find("input").val();
			var hcdrugMateFeeH=$(n).find("td").eq(15).find("input").val();
			str += "HCDrugMateFee:"+hcdrugMateFeeL+":"+hcdrugMateFeeH;
			str += ",";
		}
	})
	str = str.substr(0, str.length-1)
	openTab('/pages/forecast/forecastDepDis.jsp', depDesc+"|病种参数配置", 'forecastParam'+depCode, 'forecastParam'+depCode, {depCode:depCode, param:str});
}